<?php

$FamilyId = @$_GET['FamilyId'];
//$IndividualId = @$_GET['IndividualId']; // not valid
$submitData = @$_POST['submitData'];

echo '<div id="page_header">'."\n";
echo '<ul>'."\n";
echo '<li class="titlename2">Add new Family</li>'."\n";
echo '</ul>'."\n";
echo '</div>'."\n";

if($PACTS_cfg->getUserCanAdd()) {

    if($submitData == "Add New Family")
    {
        $PACTS_cfg->PMA_gpc_extract($HTTP_POST_VARS, $GLOBALS);
        //	$IndividualId = $fields['IndividualId'];

        $fields_changes = array();
        $fields_changes_prev = array();
        $fields_invalid = array();
        $fields_changes_SQL = array();

        // check fields for validity
        $bInvalidField = FALSE;
    } else {
        $fields['HomePhoneInternationalCode'] = $PACTS_cfg->intDefaultInternationalTelephoneCode;
        $fields['HomePhoneStdCode'] = $PACTS_cfg->intDefaultTelephoneSTDCode;
        $fields['ResidentialCountry'] = $PACTS_cfg->DefaultCountry ;
    }

    $fields['FamilyShortName'] = trim(@$fields['FamilyShortName']);

    if(strlen($fields['FamilyShortName'])>0) {
        if(!ctype_alpha($fields['FamilyShortName'][0]) ) {
            echo "<b>Error!: FamilyShortName must start with a Alphabetic character</b><p>";
            $fields['FamilyShortName']="";
        }
    }

    if( strlen(trim(@$fields['FamilyShortName']))>0 ) //and strlen(trim(@$fields['HomePhoneNumber']))>0
    {
        if($submitData == "Add New Family") {

            if (strlen(trim(@$fields['HomePhoneNumber']))>0){
                $insertSql = "INSERT INTO ".$PACTS_cfg->familytablename." (FamilyShortName,
                HomePhoneInternationalCode,
                HomePhoneStdCode,
                HomePhoneNumber,
                ResidentialAddressPrivateIndicator ,
                PostalAddressPrivateIndicator ,
                HomePhoneNumberPrivateIndicator ,
                HomeFaxNumberPrivateIndicator,
                MailingAddressIndicator,
                ResidentialCountry,
                PostalAddressCountry ,
                CreationTMS,
                CreationUserId,
                ModificationTMS,
                ModificationUserId)
                VALUES('".trim($fields['FamilyShortName'])."' , '".$fields['HomePhoneInternationalCode']."', '".$fields['HomePhoneStdCode']."', '".$fields['HomePhoneNumber']."', 'N', 'N', 'N', 'N', 'N', '".$fields['ResidentialCountry']."',
                '".$fields['ResidentialCountry']."',
                now(), '".$_SESSION['username']."', now(),'".$_SESSION['username']."')";
            } else {
                $insertSql = "INSERT INTO ".$PACTS_cfg->familytablename." (FamilyShortName,
                HomePhoneInternationalCode,
                HomePhoneStdCode,
                ResidentialAddressPrivateIndicator ,
                PostalAddressPrivateIndicator ,
                HomePhoneNumberPrivateIndicator ,
                HomeFaxNumberPrivateIndicator,
                MailingAddressIndicator,
                ResidentialCountry,
                PostalAddressCountry ,
                CreationTMS,
                CreationUserId,
                ModificationTMS,
                ModificationUserId)
                VALUES('".trim($fields['FamilyShortName'])."' , '".$fields['HomePhoneInternationalCode']."', '".$fields['HomePhoneStdCode']."', 'N', 'N', 'N', 'N', 'N', '".$fields['ResidentialCountry']."',
                '".$fields['ResidentialCountry']."',
                now(), '".$_SESSION['username']."', now(),'".$_SESSION['username']."')";
            }


            //		echo "<p>$insertSql <p>";
            //exit;
            $result_insert = mysql_query($insertSql,$PACTS_cfg->dblink);
            $FamilyId = mysql_insert_id();
            //		   echo "<p> R = ".$FamilyId ;
            //		   echo "<p> Error No = ".mysql_errno();
            if(!mysql_errno()) {

                $PACTS_cfg->InsertAuditTrailEntry("", "" ,"INSERT","Family","COMPLETE ENTRY",$_SESSION['username'], $_SERVER['PHP_SELF'],$FamilyId);
                foreach ($fields as $fields_key => $fields_value){
                    if (strlen(trim($fields_value)) > 0 ){
                        $PACTS_cfg->InsertAuditTrailEntry("", $fields_value ,"Add","Family",$fields_key,$_SESSION['username'], $_SERVER['PHP_SELF'],$FamilyId);
                    }
                }
                $URLmessage="Family added sucessfully!";

                $URL = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/index.php?view=Fmly_view&FamilyId=".$FamilyId."&URLmessage=".$URLmessage."";
//                header($URL);

        echo '<script type="text/javascript">'."\n";
        echo '<!--'."\n";
        echo 'window.location = "'.$URL.'"'."\n";


        echo '//-->'."\n";
        echo '</script>'."\n";
        exit;


            }
            else {
                echo "Error occured while adding Family";
                $FamilyId = null;
            }
        }
    } else
    {
        if($submitData == "Add New Family")
        {
            echo "<b>You must enter a FamilyShortName and a HomePhoneNumber!<p><b>";
        }
    }


    if(is_null($FamilyId))
    {
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


        echo '<div id="details-section">'."\n";
        echo '<form method="post" enctype="multipart/form-data" action="index.php?view=Fmly_add">';
        echo '<div id="viewDetailsC2">';

        echo '<ul>'."\n";
        echo '<li class="viewDetailsHeader" title="'.trim($PACTS_cfg->getFieldDescription("Family", "FamilyShortName")).'" >FamilyShortName*</li>'."\n";
        echo '<li class="viewDetailsDataEdit"><input name="fields[FamilyShortName]" size="" value="'.@$fields['FamilyShortName'].'" /></li>'."\n";
        echo '</ul>'."\n";
        echo '<ul>'."\n";
        echo '<li class="viewDetailsHeader" title="'.trim($PACTS_cfg->getFieldDescription($PACTS_cfg->familytablename, "HomePhoneInternationalCode")).'" >HomePhoneInternationalCode</li>'."\n";
        echo '<li class="viewDetailsDataEdit">'."\n";
        echo '<input type="text" name="fields[HomePhoneInternationalCode]" value="'.@$fields['HomePhoneInternationalCode'].'" size="3" maxlength="3" class="textbox"  />'."\n";
        echo '</li></ul>'."\n";
        echo '<ul>'."\n";
        echo '<li class="viewDetailsHeader" title="'.trim($PACTS_cfg->getFieldDescription($PACTS_cfg->familytablename, "HomePhoneStdCode")).'" >HomePhoneStdCode</li>'."\n";
        echo '<li class="viewDetailsDataEdit">'."\n";
        echo '<input type="text" name="fields[HomePhoneStdCode]" size="3" maxlength="3"  class="textbox" value="'.@$fields['HomePhoneStdCode'].'" />'."\n";
        echo '</li>'."\n";
        echo '</ul><ul>'."\n";
        echo '<li class="viewDetailsHeader" title="'.trim($PACTS_cfg->getFieldDescription($PACTS_cfg->familytablename, "HomePhoneNumber")).'" >HomePhoneNumber</li>'."\n";
        echo '<li class="viewDetailsDataEdit">'."\n";
        echo '<input type="text" name="fields[HomePhoneNumber]" size="10" maxlength="10" class="textbox" value="'.@$fields['HomePhoneNumber'].'" />'."\n";
        echo '</li>'."\n";
        echo '</ul>'."\n";


        echo '<ul><li class="viewDetailsHeader" title="'.trim($PACTS_cfg->getFieldDescription($PACTS_cfg->familytablename, "ResidentialCountry")).'" >ResidentialCountry</li>';
        echo '<li class="viewDetailsDataEdit">'."\n";
/*
        echo "(".$PACTS_cfg->aFieldLookUpTableList[$PACTS_cfg->residentialcountrytablename]['Description']."";
        if(@strlen($PACTS_cfg->aFieldLookUpTableList[$PACTS_cfg->residentialcountrytablename]['Description2'])>0) {
            echo ", ".@$PACTS_cfg->aFieldLookUpTableList[$PACTS_cfg->residentialcountrytablename]['Description2']."";
        }
        echo ")</font><br />";
*/
        echo "".$PACTS_cfg->getLookupTableHTML($PACTS_cfg->residentialcountrytablename,"fields[ResidentialCountry]",@$fields['ResidentialCountry']);
        echo "\n";
        echo "</li></ul>\n";

        echo '<ul><li class="viewDetailsHeader"></li>'."\n";
        echo '<li>'."\n";
        echo '<input type="submit" name="submitData" value="Add New Family" class="smalltxtInput" />';
        echo "</li></ul>\n";
        echo "</div>";
        echo "</form>";
        echo "<p class=\"smalltxt\">* Mandatory Fields</p>";
    echo '</div>'."\n";
    }
} 
else {

    echo '<div id="details-section">'."\n";
    echo '<div id="viewDetailsC2">'."\n";

    echo '<p class="errmsg1">Error! You do not have permission to add data!</p>'."\n";
    echo '</div>'."\n";
    echo '</div>'."\n";

} 


?>
